package com.zongzi.stock.module.stockdetail;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.SparseArray;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.highlight.Highlight;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.github.mikephil.charting.listener.OnChartValueSelectedListener;
import com.github.mikephil.charting.utils.Utils;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.orhanobut.logger.Logger;
import com.zongzi.stock.R;
import com.zongzi.stock.bean.DailyBean;
import com.zongzi.stock.bean.DailyDataBean;
import com.zongzi.stock.global.URLs;
import com.zongzi.stock.utils.MathUtils;
import com.zongzi.stock.utils.TimeUtils;
import com.zongzi.stock.view.mychart.LargeValueFormatter;
import com.zongzi.stock.view.mychart.MyBarChart;
import com.zongzi.stock.view.mychart.MyLineChart;
import com.zongzi.stock.view.mychart.MyXAxis;
import com.zongzi.stock.view.mychart.MyYAxis;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;
import rx.Subscription;

import static com.zongzi.stock.module.HomeActivity.ERROR_MSG;

public class StockDetailActivity extends AppCompatActivity {

    @BindView(R.id.tv_price_now)
    TextView mTvPriceNow;
    @BindView(R.id.tv_price_error)
    TextView mTvPriceError;
    @BindView(R.id.tv_price_ratio)
    TextView mTvPriceRatio;
    @BindView(R.id.tv_price_highest)
    TextView mTvPriceHighest;
    @BindView(R.id.tv_price_lowest)
    TextView mTvPriceLowest;
    @BindView(R.id.tv_price_open)
    TextView mTvPriceOpen;
    @BindView(R.id.tv_price_change)
    TextView mTvPriceChange;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_num)
    TextView mTvNum;
    @BindView(R.id.line_chart)
    MyLineChart mLineChart;
    @BindView(R.id.bar_chart)
    MyBarChart mBarChart;
    @BindView(R.id.tv_chart_time)
    TextView mTvChartTime;
    @BindView(R.id.tv_chart_average_price)
    TextView mTvChartAveragePrice;
    @BindView(R.id.tv_chart_price)
    TextView mTvChartPrice;
    @BindView(R.id.tv_chart_percent_price)
    TextView mTvChartPercentPrice;
    @BindView(R.id.ll_chart)
    LinearLayout mLlChart;
    @BindView(R.id.tv_chart_percent_price_des)
    TextView mTvChartPercentPriceDes;

    private String stockNum;
    private Gson gson;
    private List<Entry> lineEntries;
    private List<Entry> lineEntriesAv;
    private List<BarEntry> barEntries;
    private DailyBean dailyBean;
    private DailyDataBean mDailyDataBean;

    private boolean isNameSet = false;
    private DecimalFormat mFormat = new DecimalFormat("######0.00");

    private Subscription subscriptionMinute;
    private LineDataSet LineDataSetPrice, LineDataSetAve;
    private MyXAxis xAxisLine;
    private MyYAxis axisRightLine;
    private MyYAxis axisLeftLine;

    private BarDataSet barDataSet;
    private XAxis xAxisBar;
    private MyYAxis axisLeftBar;
    private MyYAxis axisRightBar;
    private SparseArray<String> stringSparseArray;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stock_detail);
        ButterKnife.bind(this);

        gson = new Gson();
        stringSparseArray = setXLabels();

        Intent intent = getIntent();
        String stockName = intent.getStringExtra("name");
        stockNum = intent.getStringExtra("num");

        mTvName.setText(stockName);
        mTvNum.setText(stockNum.substring(2, stockNum.length()));

        lineEntries = new ArrayList<>();
        lineEntriesAv = new ArrayList<>();
        barEntries = new ArrayList<>();

        initChart();

        updateData();

    }

    public void updateDailyData(String result) {
        if (result.charAt(0) == '{' && result.charAt(result.length() - 1) == '}') {
            mDailyDataBean = gson.fromJson(result, DailyDataBean.class);
            if (mDailyDataBean != null && mDailyDataBean.errorMsg.equals(ERROR_MSG)
                    && mDailyDataBean.data != null && mDailyDataBean.data.size() >= 0) {
                if (!isNameSet) {
                    mTvName.setText(mDailyDataBean.data.get(0).stockName);
                    mTvNum.setText(mDailyDataBean.data.get(0).stockCode);
                    isNameSet = true;
                }
                if (MathUtils.nRound((mDailyDataBean.data.get(0).high
                        - mDailyDataBean.data.get(0).preClose), 2) > 0) {
                    mTvPriceHighest.setTextColor(getResources().getColor(R.color.tv_stock_increase));
                } else if (MathUtils.nRound((mDailyDataBean.data.get(0).high
                        - mDailyDataBean.data.get(0).preClose), 2) == 0) {
                    mTvPriceHighest.setTextColor(getResources().getColor(R.color.tv_stock_normal));
                } else {
                    mTvPriceHighest.setTextColor(getResources().getColor(R.color.tv_stock_decrease));
                }
                if (MathUtils.nRound((mDailyDataBean.data.get(0).low
                        - mDailyDataBean.data.get(0).preClose), 2) > 0) {
                    mTvPriceLowest.setTextColor(getResources().getColor(R.color.tv_stock_increase));
                } else if (MathUtils.nRound((mDailyDataBean.data.get(0).low
                        - mDailyDataBean.data.get(0).preClose), 2) == 0) {
                    mTvPriceLowest.setTextColor(getResources().getColor(R.color.tv_stock_normal));
                } else {
                    mTvPriceLowest.setTextColor(getResources().getColor(R.color.tv_stock_decrease));
                }
                if (MathUtils.nRound((mDailyDataBean.data.get(0).open
                        - mDailyDataBean.data.get(0).preClose), 2) > 0) {
                    mTvPriceOpen.setTextColor(getResources().getColor(R.color.tv_stock_increase));
                } else if (MathUtils.nRound((mDailyDataBean.data.get(0).open
                        - mDailyDataBean.data.get(0).preClose), 2) == 0) {
                    mTvPriceOpen.setTextColor(getResources().getColor(R.color.tv_stock_normal));
                } else {
                    mTvPriceOpen.setTextColor(getResources().getColor(R.color.tv_stock_decrease));
                }
                mTvPriceChange.setTextColor(getResources().getColor(R.color.tv_stock_normal));
                mTvPriceHighest.setText(MathUtils.nRound(mDailyDataBean.data.get(0).high, 2) + "");
                mTvPriceLowest.setText(MathUtils.nRound(mDailyDataBean.data.get(0).low, 2) + "");
                mTvPriceOpen.setText(MathUtils.nRound(mDailyDataBean.data.get(0).open, 2) + "");
                mTvPriceChange.setText(MathUtils.nRound(mDailyDataBean.data.get(0).turnoverRatio, 2) + "%");

                if (MathUtils.nRound((mDailyDataBean.data.get(0).close
                        - mDailyDataBean.data.get(0).preClose), 2) > 0) {
                    mTvPriceNow.setTextColor(getResources().getColor(R.color.tv_stock_increase));
                    mTvPriceError.setTextColor(getResources().getColor(R.color.tv_stock_increase));
                    mTvPriceRatio.setTextColor(getResources().getColor(R.color.tv_stock_increase));
                } else if (MathUtils.nRound((mDailyDataBean.data.get(0).close
                        - mDailyDataBean.data.get(0).preClose), 2) == 0) {
                    mTvPriceNow.setTextColor(getResources().getColor(R.color.tv_stock_normal));
                    mTvPriceError.setTextColor(getResources().getColor(R.color.tv_stock_normal));
                    mTvPriceRatio.setTextColor(getResources().getColor(R.color.tv_stock_normal));
                } else {
                    mTvPriceNow.setTextColor(getResources().getColor(R.color.tv_stock_decrease));
                    mTvPriceError.setTextColor(getResources().getColor(R.color.tv_stock_decrease));
                    mTvPriceRatio.setTextColor(getResources().getColor(R.color.tv_stock_decrease));
                }
                mTvPriceNow.setText(MathUtils.nRound(mDailyDataBean.data.get(0).close, 2) + "");
                mTvPriceError.setText(MathUtils.nRound(mDailyDataBean.data.get(0).netChange, 2) + "");
                mTvPriceRatio.setText(MathUtils.nRound(mDailyDataBean.data.get(0).netChangeRatio, 2) + "%");
            }
        }
    }

    public void updateData() {
        try {
            Logger.d(URLs.getDailyData(stockNum));
            OkGo.get(URLs.getDailyData(stockNum))     // 请求方式和请求url
                    .tag(this)                       // 请求的 tag, 主要用于取消对应的请求
                    .cacheMode(CacheMode.NO_CACHE)    // 缓存模式，详细请看缓存介绍
                    .execute(new StringCallback() {

                        @Override
                        public void onSuccess(String s, Call call, Response response) {
                            // s 即为所需要的结果
                            Logger.d("网络加载成功，加载当天数据：" + s);
                            updateDailyData(s);
                        }

                        @Override
                        public void onError(Call call, Response response, Exception e) {
                            super.onError(call, response, e);
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

//        Logger.d("获取分时曲线数据");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    //currentMillis = System.currentTimeMillis();
                    Response response = OkGo.get(URLs.getMinData(stockNum))//
                            .tag(this)//
                            .execute();
                    String result = response.body().string();
                    Logger.d(URLs.getMinData(stockNum) + "\n"
                            + "网络加载成功，更新数据：" + result);
                    if (result.charAt(0) == '{' && result.charAt(result.length() - 1) == '}') {
                        dailyBean = gson.fromJson(result, DailyBean.class);
                        if (dailyBean != null && dailyBean.errorMsg.equals(ERROR_MSG)
                                && dailyBean.timeLine != null && dailyBean.timeLine.size() >= 0) {
                            for (int i = 0; i < (dailyBean.timeLine.size() - getMinutesStart()); i++) {
                                lineEntries.add(new Entry((float) i
                                        , (float) MathUtils.nRound(dailyBean.timeLine.get(i + getMinutesStart()).price, 2)));
                                lineEntriesAv.add(new Entry((float) i
                                        , (float) MathUtils.nRound(dailyBean.timeLine.get(i + getMinutesStart()).avgPrice, 2)));

                                barEntries.add(new BarEntry((float) i
                                        , (float) dailyBean.timeLine.get(i + getMinutesStart()).volume / 100f));
                            }
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    //在主界面更新数据
                                    setData();
                                }
                            });
                        }
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    public void setData() {

        if (mDailyDataBean != null && mDailyDataBean.errorMsg.equals(ERROR_MSG)
                && mDailyDataBean.data != null && mDailyDataBean.data.size() >= 0) {
            double highPrice = mDailyDataBean.data.get(0).high;
            double lowPrice = mDailyDataBean.data.get(0).low;
            double preClosePrice = mDailyDataBean.data.get(0).preClose;
            double error = Math.max(Math.abs(highPrice - preClosePrice), Math.abs(lowPrice - preClosePrice));
            //设置y左右两轴最大最小值
            axisLeftLine.setAxisMaximum((float) (preClosePrice + error));
            axisLeftLine.setAxisMinimum((float) (preClosePrice - error));

            axisRightLine.setAxisMaximum((float) (error * 100 / preClosePrice));
            axisRightLine.setAxisMinimum((float) (-error * 100 / preClosePrice));
        }

        axisRightBar.setAxisMaximum((float) dailyBean.getMaxVolume() / 100f);

        //基准线
        LimitLine ll = new LimitLine(0);
        ll.setLineWidth(1f);
        ll.setLineColor(getResources().getColor(R.color.minute_black));
        ll.enableDashedLine(10f, 10f, 0f);
        ll.setLineWidth(1);
        axisRightLine.addLimitLine(ll);
        axisRightLine.setBaseValue(0);


        // add entries to dataset
        LineDataSetPrice = new LineDataSet(lineEntries, "成交价");
        LineDataSetPrice.setColor(Color.BLACK);
        //dataSet.setValueTextColor(...); // styling, ...
        LineDataSetPrice.setDrawCircles(false);
        LineDataSetPrice.setDrawValues(false);
        LineDataSetPrice.setCircleRadius(0);
        LineDataSetPrice.setHighLightColor(Color.BLACK);
        //谁为基准
        LineDataSetPrice.setAxisDependency(YAxis.AxisDependency.LEFT);
//        lineDataSet.setDrawFilled(true);
        LineDataSetAve = new LineDataSet(lineEntriesAv, "均价");
        LineDataSetAve.setDrawCircles(false);
        LineDataSetAve.setDrawValues(false);
        LineDataSetAve.setCircleRadius(0);
        LineDataSetAve.setColor(getResources().getColor(R.color.average_price));
        LineDataSetAve.setHighlightEnabled(false);

        ArrayList<ILineDataSet> sets = new ArrayList<ILineDataSet>();
        sets.add(LineDataSetPrice);
        sets.add(LineDataSetAve);
        LineData lineData = new LineData(sets);
        mLineChart.setData(lineData);

        barDataSet = new BarDataSet(barEntries, "成交量");
        barDataSet.setHighLightColor(Color.BLACK);
        barDataSet.setHighLightAlpha(255);
        barDataSet.setDrawValues(false);
        barDataSet.setHighlightEnabled(true);
        //barDataSet.setColor(Color.RED);
        List<Integer> list = new ArrayList<>();
        list.add(getResources().getColor(R.color.tv_stock_increase));
        list.add(getResources().getColor(R.color.tv_stock_decrease));
        barDataSet.setColors(list);
        BarData barData = new BarData(barDataSet);
        barData.setBarWidth(0.9f); // set custom bar width
        mBarChart.setData(barData);
        mBarChart.setFitBars(true); // make the x-axis fit exactly all bars

        mBarChart.invalidate();//刷新图
        mLineChart.invalidate();
    }


    private void initChart() {

        mLineChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                mBarChart.highlightValues(new Highlight[]{h});
                mLlChart.setVisibility(View.VISIBLE);

                int x = (int) e.getX() + getMinutesStart();
                Logger.d(x);
                mTvChartTime.setText(TimeUtils.getTimeString(dailyBean.timeLine.get(x).time));
                mTvChartAveragePrice.setText(MathUtils.nRound(dailyBean.timeLine.get(x).avgPrice, 2) + "");
                mTvChartPrice.setText(MathUtils.nRound(dailyBean.timeLine.get(x).price, 2) + "");
                if (dailyBean.timeLine.get(x).netChangeRatio > 0) {
                    mTvChartPercentPriceDes.setTextColor(getResources().getColor(R.color.tv_stock_increase));
                    mTvChartPercentPrice.setTextColor(getResources().getColor(R.color.tv_stock_increase));

                } else if (dailyBean.timeLine.get(x).netChangeRatio < 0) {
                    mTvChartPercentPriceDes.setTextColor(getResources().getColor(R.color.tv_stock_decrease));
                    mTvChartPercentPrice.setTextColor(getResources().getColor(R.color.tv_stock_decrease));
                } else {
                    mTvChartPercentPriceDes.setTextColor(getResources().getColor(R.color.tv_stock_normal));
                    mTvChartPercentPrice.setTextColor(getResources().getColor(R.color.tv_stock_normal));
                }
                mTvChartPercentPrice.setText(MathUtils.nRound(dailyBean.timeLine.get(x).netChangeRatio, 2) + "%");
            }

            @Override
            public void onNothingSelected() {
                mLlChart.setVisibility(View.INVISIBLE);
                mBarChart.highlightValue(null);
            }
        });

        mBarChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
            @Override
            public void onValueSelected(Entry e, Highlight h) {
                mLineChart.highlightValues(new Highlight[]{h});
                mLlChart.setVisibility(View.VISIBLE);

                int x = (int) e.getX() + getMinutesStart();
                Logger.d(x);
//                mTvChartTime.setText(dailyBean.timeLine.get(x).time);
                mTvChartAveragePrice.setText(MathUtils.nRound(dailyBean.timeLine.get(x).avgPrice, 2) + "");
                mTvChartPrice.setText(MathUtils.nRound(dailyBean.timeLine.get(x).price, 2) + "");
                if (dailyBean.timeLine.get(x).netChangeRatio > 0) {
                    mTvChartPercentPriceDes.setTextColor(getResources().getColor(R.color.tv_stock_increase));
                    mTvChartPercentPrice.setTextColor(getResources().getColor(R.color.tv_stock_increase));

                } else if (dailyBean.timeLine.get(x).netChangeRatio < 0) {
                    mTvChartPercentPriceDes.setTextColor(getResources().getColor(R.color.tv_stock_decrease));
                    mTvChartPercentPrice.setTextColor(getResources().getColor(R.color.tv_stock_decrease));
                } else {
                    mTvChartPercentPriceDes.setTextColor(getResources().getColor(R.color.tv_stock_normal));
                    mTvChartPercentPrice.setTextColor(getResources().getColor(R.color.tv_stock_normal));
                }
                mTvChartPercentPrice.setText(MathUtils.nRound(dailyBean.timeLine.get(x).netChangeRatio, 2) + "%");
            }

            @Override
            public void onNothingSelected() {
                mLlChart.setVisibility(View.INVISIBLE);
                mLineChart.highlightValue(null);
            }
        });

        mLineChart.setScaleEnabled(false);
        mLineChart.setDrawBorders(true);
        mLineChart.setBorderWidth(1);
        mLineChart.setBorderColor(getResources().getColor(R.color.minute_grayLine));
        Description lineDescription = new Description();
        lineDescription.setText("");
        mLineChart.setDescription(lineDescription);
        Legend lineChartLegend = mLineChart.getLegend();
        lineChartLegend.setEnabled(false);

        mBarChart.setScaleEnabled(false);
        mBarChart.setDrawBorders(true);
        mBarChart.setBorderWidth(1);
        mBarChart.setBorderColor(getResources().getColor(R.color.minute_grayLine));
        Description barDescription = new Description();
        barDescription.setText("");
        mBarChart.setDescription(barDescription);
        Legend barChartLegend = mBarChart.getLegend();
        barChartLegend.setEnabled(false);

        //x轴
        xAxisLine = mLineChart.getXAxis();
        xAxisLine.setLabelCount(5, true);
        xAxisLine.setDrawLabels(true);
        xAxisLine.setAxisMaximum(getMinutesCount());
        xAxisLine.setPosition(XAxis.XAxisPosition.BOTTOM);
        // xAxisLine.setLabelsToSkip(59);

        xAxisLine.setXLabels(stringSparseArray);

        //左边y
        axisLeftLine = mLineChart.getAxisLeft();
        /*折线图y轴左没有basevalue，调用系统的*/
        axisLeftLine.setLabelCount(3, true);
        axisLeftLine.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        axisLeftLine.setDrawLabels(true);
        axisLeftLine.setDrawGridLines(false);
        /*轴不显示 避免和border冲突*/
        axisLeftLine.setDrawAxisLine(false);
        axisLeftLine.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return mFormat.format(value);
            }
        });


        //右边y
        axisRightLine = mLineChart.getAxisRight();
        axisRightLine.setLabelCount(2, true);
        axisRightLine.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        axisRightLine.setDrawLabels(true);
//        axisRightLine.setStartAtZero(false);
        axisRightLine.setDrawGridLines(false);
        axisRightLine.setDrawAxisLine(false);
        axisRightLine.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return mFormat.format(value) + "%";
            }
        });


        //背景线
        xAxisLine.setGridColor(getResources().getColor(R.color.minute_grayLine));
        xAxisLine.enableGridDashedLine(10f, 5f, 0f);
        xAxisLine.setAxisLineColor(getResources().getColor(R.color.minute_grayLine));
        xAxisLine.setTextColor(getResources().getColor(R.color.minute_black));
        axisLeftLine.setGridColor(getResources().getColor(R.color.minute_grayLine));
        axisLeftLine.setTextColor(getResources().getColor(R.color.minute_zhoutv));
//        axisRightLine.setAxisLineColor(getResources().getColor(R.color.minute_grayLine));
//        axisRightLine.setTextColor(getResources().getColor(R.color.minute_zhoutv));


        //bar x y轴
        xAxisBar = mBarChart.getXAxis();
        xAxisBar.setAxisMaximum(getMinutesCount());
        xAxisBar.setLabelCount(5, true);
        xAxisBar.setDrawLabels(false);
        xAxisBar.setDrawGridLines(true);
        xAxisBar.setDrawAxisLine(false);
        xAxisBar.setGranularityEnabled(false);
//        xAxisBar.setGranularity(1f);
        // xAxisBar.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxisBar.setGridColor(getResources().getColor(R.color.minute_grayLine));
        xAxisBar.enableGridDashedLine(10f, 5f, 0f);


        axisLeftBar = mBarChart.getAxisLeft();
//        axisLeftBar.setAxisMinValue(0);
//        axisLeftLine.setLabelCount(3, true);
//        axisLeftBar.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        axisLeftBar.setDrawLabels(false);
        axisLeftBar.setDrawGridLines(false);
        axisLeftBar.setDrawAxisLine(false);
        axisLeftBar.setTextColor(getResources().getColor(R.color.minute_zhoutv));


        axisRightBar = mBarChart.getAxisRight();
        axisRightBar.setAxisMinValue(0);
        axisRightBar.setLabelCount(2, true);
        axisRightBar.setPosition(YAxis.YAxisLabelPosition.INSIDE_CHART);
        axisRightBar.setDrawLabels(true);
        axisRightBar.setDrawGridLines(false);
        axisRightBar.setDrawAxisLine(false);

        axisRightBar.setShowMaxAndUnit(" ");
        axisRightBar.setValueFormatter(new LargeValueFormatter("手"));

    }

    /*设置量表对齐*/
    private void setOffset() {
        float lineLeft = mLineChart.getViewPortHandler().offsetLeft();
        float barLeft = mBarChart.getViewPortHandler().offsetLeft();
        float lineRight = mLineChart.getViewPortHandler().offsetRight();
        float barRight = mBarChart.getViewPortHandler().offsetRight();
        float barBottom = mBarChart.getViewPortHandler().offsetBottom();
        float offsetLeft, offsetRight;
        float transLeft = 0, transRight = 0;

        /*注：setExtraLeft...函数是针对图表相对位置计算，比如A表offLeftA=20dp,B表offLeftB=30dp,
         则A.setExtraLeftOffset(10),并不是30，还有注意单位转换*/
        if (barLeft < lineLeft) {
            //offsetLeft = Utils.convertPixelsToDp(lineLeft - barLeft);
            // barChart.setExtraLeftOffset(offsetLeft);
            transLeft = lineLeft;

        } else {
            offsetLeft = Utils.convertPixelsToDp(barLeft - lineLeft);
            mLineChart.setExtraLeftOffset(offsetLeft);
            transLeft = barLeft;
        }

        /*注：setExtraRight...函数是针对图表绝对位置计算，比如A表offRightA=20dp,
          B表offRightB=30dp,则A.setExtraLeftOffset(30),并不是10，还有注意单位转换*/
        if (barRight < lineRight) {
            //offsetRight = Utils.convertPixelsToDp(lineRight);
            //barChart.setExtraRightOffset(offsetRight);
            transRight = lineRight;
        } else {
            offsetRight = Utils.convertPixelsToDp(barRight);
            mLineChart.setExtraRightOffset(offsetRight);
            transRight = barRight;
        }
        mBarChart.setViewPortOffsets(transLeft, 5, transRight, barBottom);
    }

    private SparseArray<String> setXLabels() {
        SparseArray<String> xLabels = new SparseArray<>();
        xLabels.put(0, "09:30");
        xLabels.put(60, "10:30");
        xLabels.put(121, "11:30/13:00");
        xLabels.put(182, "14:00");
        xLabels.put(241, "15:00");
        return xLabels;
    }

    public int getMinutesCount() {
        return 241;
    }

    public int getMinutesStart() {
        return 14;
    }

}
